﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.Model;
using PPAP.BO;

namespace PPAP.Web.Supplier
{
    public partial class ContrctPersonEdit : PageBase
    {
        ContractPersonBO bo = new ContractPersonBO();
        LogBO lbo = new LogBO();
        UsersBO ubo = new UsersBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["CID"] != null)
                {
                    LoadCon(Request.QueryString["CID"].ToString());
                }

            }
        }

        private void LoadCon(string id)
        {
            ContractPerson p = bo.GetModel(int.Parse(Request.QueryString["CID"].ToString()));
            txtEmail.Text = p.EMAIL;
            txtFirstName.Text = p.Firstname;
            txtTelephone.Text = p.TELEPHONE;
            rbtfemale.Checked = p.GENDER.Trim() == "female" ? true : false;
            rbtInactive.Checked = p.STATUS.Trim() == "inactive" ? true : false;
            ddlPostition.SelectedValue = p.POSTITION;
            txtFamilyName.Text = p.NAME;
            Button1.Text = "Save";
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (txtFirstName.Text.Trim() == "" || txtEmail.Text.Trim() == ""||txtFamilyName.Text.Trim()=="")
            {
                AlertMessage("The text frame not is empty");
                return;
            }
            if (Request.QueryString["RID"] == null && Session["SupplierInfo"] == null)
            {
                AlertMessage("No SupplierInfo");
            }
            else
            {
                int num = 0;
                ContractPerson p = new ContractPerson();
                if (Request.QueryString["CID"] != null)
                    p = bo.GetModel(int.Parse(Request.QueryString["CID"].ToString()));
                p.EMAIL = txtEmail.Text;
                p.GENDER = rbtMale.Checked ? "male" : "female";
                p.NAME = txtFamilyName.Text;
                p.Firstname = txtFamilyName.Text;
                p.POSTITION = ddlPostition.SelectedValue;
                p.STATUS = rbtActive.Checked ? "active" : "inactive";
                p.TELEPHONE = txtTelephone.Text;
                p.Plantid = Request.QueryString["PID"] != null ? int.Parse(Request.QueryString["PID"].ToString()) : 0;
               
                if (Button1.Text == "Add")
                {
                    p.Password = "123456";
                    if (ubo.CheckEamil(p.EMAIL))
                    {
                        AlertMessage("Email already exists");
                        return;
                    }
                    if (bo.GetList(" Email='" + p.EMAIL + "'").Tables[0].Rows.Count > 0)
                    {
                        AlertMessage("Email already exists");
                        return;
                    }
                    num = bo.Add(p);
                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Add ContractPerson:" + p.NAME);
                        AlertReflashOpener("Save success");
                    }
                }
                else if (Button1.Text == "Save")
                {
                    num = bo.Update(p);
                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Edit ContractPerson:" + p.NAME);
                        Button1.Text = "Add";
                        AlertReflashOpener("Save success");
                    }
                }
                txtEmail.Text = "";
                //txtName.Text = "";
                txtTelephone.Text = "";
            }
        }

        public string GetPhone(object ico)
        {
            return "ico/" + ico.ToString().Trim() + ".png";
        }
    }
}